
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'


Page({
  data: {
    avatarUrl: defaultAvatarUrl,
    theme: wx.getSystemInfoSync().theme,
    // 昵称
    username:"",
  },
  onLoad() {
    let lsbl = wx.getStorageSync('userinfo')
    if(lsbl!=undefined||lsbl!=''){
      // 不等于空的情况下把值传递到data
      this.setData({
        avatarUrl:lsbl.avatarUrl,
        username:lsbl.username
      })
    }
    wx.onThemeChange((result) => {
      this.setData({
        theme: result.theme
      })
    })
  },
  toIndex(){
    wx.switchTab({
      url: '../../pages/my/my',
    })
  },
  onChooseAvatar(e) {
    console.log(e)
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
  },
  // 确定保存
  qd(){
    console.log(this.data.avatarUrl)
    console.log(this.data.username)
    let openid = wx.getStorageSync('openid').openid
    wx.request({
      method:'POST',
      url: `https://watermark.czmao.cn:3000/updataUser?openid=${openid}&nickname=${this.data.username}&user_img=${this.data.avatarUrl}`,
      success:(res)=>{
        console.log('修改用户数据成功')
      },
      fail:(err)=>{
        console.log('修改用户数据失败')
      }
    })
    if(this.data.username==""){
      this.data.username="微信用户"
    }
    let userinfo={
      avatarUrl:this.data.avatarUrl,
      username:this.data.username
    }
    // 获取到头像和昵称的数据，保存到缓存
    wx.setStorageSync('userinfo', userinfo)    
    wx.showToast({
      title: '修改成功',
      success:(res)=>{
        // wx.switchTab({
        //   url: '../../pages/my/my',
        // })
      }
    })
  }
})
